package com.zhongfeng.api.modules.quality.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongfeng.api.modules.img.mapper.ImgMapper;
import com.zhongfeng.api.modules.quality.mapper.QualityMapper;
import com.zhongfeng.api.modules.quality.service.QualityService;
import com.zhongfeng.api.modules.quality.vo.QualityVo;
import com.zhongfeng.api.modules.quality.vo.params.QualityParams;
import com.zhongfeng.api.modules.utils.function.Transfer;
import com.zhongfeng.api.modules.utils.pojo.PageDataInfo;
import com.zhongfeng.api.modules.utils.pojo.Result;
import com.zhongfeng.api.modules.utils.service.HttpStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author blueCat
 * @date 2023/2/8 10:18
 * @motto life is a gift
 */
@Service
@Slf4j
public class QualityServiceImpl implements QualityService {

    @Autowired
    private QualityMapper qualityMapper;

    @Autowired
    private ImgMapper imgMapper;

    /**
     * 查询全部质检信息
     *
     * @param params
     * @return
     */
    @Override
    public Result queryAllQuality(QualityParams params) {
        Page<QualityVo> page = new Page<>(params.getPageNum(), params.getPageSize());
        List<QualityVo> qualityVos = qualityMapper.queryAllQuality(page, params);
        return Result.setSuccess(HttpStatus.SUCCESS, "", new PageDataInfo(page, qualityVos));
    }

    /**
     * 新增质检信息
     *
     * @param params
     * @return
     */
    @Override
    public Result addQuality(QualityParams params, HttpServletRequest request) {
        //得到图片的名字
        String license = (String) request.getSession().getAttribute("imgUrl");
        //查询图片id
        Integer licenseId = imgMapper.queryImg(license);
        params.setLicenseId(licenseId);
        params.setUserId((Integer) request.getSession().getAttribute("userId"));
        request.getSession().removeAttribute("imgUrl");
        params.setIp(Transfer.combineList(params.getIpIdList()));
        int i = qualityMapper.addQuality(params);
        if (i == 1) {
            return Result.setSuccess(HttpStatus.SUCCESS, "添加质检信息成功！");
        } else {
            return Result.setFail(HttpStatus.FAIL, "添加质检信息失败！");
        }
    }

//    /**
//     * 删除客户信息
//     *
//     * @param id
//     * @return
//     */
//    @Override
//    public Result deleteCustomer(Integer id) {
//        int i = customerMapper.deleteCustomer(id);
//        if (i == 1) {
//            return Result.setSuccess(HttpStatus.SUCCESS, "删除客户成功！");
//        } else {
//            return Result.setFail(ReturnCodes.DELETE_CUSTOMER_FAIL, ReturnMessage.DELETE_CUSTOMER_FAIL);
//        }
//    }
//
//    /**
//     * 更新客户信息
//     *
//     * @param params
//     * @return
//     */
//    @Override
//    public Result updateCustomer(CustomerParams params, HttpServletRequest request) {
//        String license = "";
//        if (params.getLicense() != null) {
//            license = params.getLicense();
//        } else {
//            license = (String) request.getSession().getAttribute("imgUrl");
//        }
//        //查询图片id
//        Integer licenseId = imgMapper.queryImg(license);
//        params.setLicenseId(licenseId);
//        log.info("这是修改的图片路径" + request.getSession().getAttribute("imgUrl"));
//        request.getSession().removeAttribute("imgUrl");
//        params.setIp(Transfer.combineList(params.getIpIdList()));
//        int i = customerMapper.updateCustomer(params);
//        if (i == 1) {
//            return Result.setSuccess(HttpStatus.SUCCESS, "修改客户成功！");
//        } else {
//            return Result.setFail(ReturnCodes.UPDATE_CUSTOMER_FAIL, ReturnMessage.UPDATE_CUSTOMER_FAIL);
//        }
//    }
}
